package com.dgj.sigar.gathers;

import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;

/**
 * 磁盘信息收集器
 * 
 * @author wkipy
 *
 */
public class DISKGather {

	private String userHome = System.getProperty("user.home");;

	private String tempDir = System.getProperty("java.io.tmpdir");

	private FileSystem[] fileSystems;

	public static DISKGather init(Sigar sigar) {
		DISKGather gather = new DISKGather();
		try {
			org.hyperic.sigar.FileSystem[] fileSystems = sigar.getFileSystemList();
			gather.fileSystems = new FileSystem[fileSystems.length];
			for (int i = 0; i < fileSystems.length; i++) {
				gather.fileSystems[i] = new FileSystem(fileSystems[i], sigar);
			}
		} catch (SigarException e) {
			e.printStackTrace();
		}
		return gather;
	}

	public String getUserHome() {
		return userHome;
	}

	public String getTempDir() {
		return tempDir;
	}

	public FileSystem[] getFileSystems() {
		return fileSystems;
	}

	/**
	 * 扩展的磁盘信息
	 * 
	 * @author wkipy
	 *
	 */
	public static class FileSystem {

		private FileSystemUsage fileSystemUsage;

		private org.hyperic.sigar.FileSystem fileSystem;

		public FileSystem(org.hyperic.sigar.FileSystem fileSystem) {
			super();
			this.fileSystem = fileSystem;
		}

		public FileSystem(org.hyperic.sigar.FileSystem fileSystem, Sigar sigar) {
			super();
			this.fileSystem = fileSystem;
			gatherUserage(sigar);
		}

		public FileSystemUsage getFileSystemUsage() {
			return fileSystemUsage;
		}

		public org.hyperic.sigar.FileSystem getFileSystem() {
			return fileSystem;
		}

		public void gatherUserage(Sigar sigar) {
			try {
				if (null == fileSystem && fileSystem.getType() == org.hyperic.sigar.FileSystem.TYPE_LOCAL_DISK) {
					fileSystemUsage = sigar.getFileSystemUsage(fileSystem.getDirName());
				}
			} catch (SigarException e) {
				e.printStackTrace();
			}
		}

	}

}
